#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime

#支持加密数据库
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from sqlalchemy import Column, Integer, Text, DateTime
from sqlalchemy.orm import declarative_base

import init
from init import session

#支持加密数据库
#engine = create_engine(init.sqlPath, echo=True)
#DbSession = sessionmaker(bind=engine)
#session = DbSession()
Base = declarative_base()

#Base = declarative_base()
#创建数据库表
#AppConfigAlchemy.metadata.create_all(init.engine)



class AppConfigAlchemy(Base):
    """
    类功能：通过数据库记录配置信息，从而实现配置信息的可编辑
    """

    __tablename__= "app_config"

    id = Column(Integer, primary_key=True, autoincrement=True, comment="Id 号")
    config_name = Column(Text, comment="配置项名称")
    config_value = Column(Text, comment="具体配置内容")
    remark = Column(Text, comment="备注")
    time_mark = Column(DateTime, comment="配置文件变更的时间"  )

    def __init__(self,
                configName = "",
                configValue = "",
                remark= "" ):

        self.config_name = configName
        self.config_value = configValue
        self.remark = remark
        self.time_mark = datetime.now()


    def AddConfigItem(self):
        '''
        函数功能：添加一条配置信息
        :return:
        '''
        session.add(self)
        session.commit()


    def GetConfigItem(self, configName):

        configInfor = session.query(AppConfigAlchemy).filter(AppConfigAlchemy.config_name==configName)\
                                                     .order_by(AppConfigAlchemy.id.desc())\
                                                     .first()

        return configInfor



def main():


    propertyAppConfigAlchemy = AppConfigAlchemy(configName = "PROPERTY_NAME_JSON",
                                                configValue = init.PROPERTY_NAME_JSON_OLD,
                                                remark = "")
    propertyAppConfigAlchemy.AddConfigItem()

    ownshipAppConfigAlchemy = AppConfigAlchemy(configName="OWNERSHIP_NAME_JSON",
                                               configValue=init.OWNERSHIP_NAME_JSON_OLD,
                                               remark="")

    ownshipAppConfigAlchemy.AddConfigItem()

    '''

    appConfigAlchemy = AppConfigAlchemy()
    result = appConfigAlchemy.GetConfigItem("OWNERSHIP_NAME_JSON")
    print(result)
    '''

# #创建数据库表
# AppConfigAlchemy.metadata.create_all(init.engine)
#AppConfigAlchemy.metadata.create_all(init.engine)

if __name__ == '__main__':
    main()